package com.ccrfid.rmc6.resource.business.prison.patrolcheck;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.entity.business.prison.patrolcheck.*;
import com.ccrfid.rmc6.entity.poi.PoiLocationEntity;
import com.ccrfid.rmc6.pojo.dto.prison.patrolcheck.PatrolCheckResultDetailDto;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.pojo.param.SortParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.service.business.prison.patrolcheck.PatrolCheckService;
import com.ccrfid.rmc6.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import javax.persistence.Basic;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

@Path("patrol-check")
@Produces(MediaType.APPLICATION_JSON)
public class PatrolCheckResource {

    @Autowired
    private PatrolCheckService patrolCheckService;

    //获取巡更任务计划时间列表
    @GET
    @Path("plan-times")
    public List<PatrolCheckPlanTimeEntity> getPatrolCheckPlanTimeList() {
        return patrolCheckService.getPatrolCheckPlanTimeList();
    }

    //新增巡更任务计划时间信息
    @POST
    @Path("plan-times/{timeValue}")
    public PatrolCheckPlanTimeEntity createPatrolCheckPlanTime(@PathParam("timeValue") Time timeValue) {
        return patrolCheckService.createPatrolCheckPlanTime(timeValue);
    }

    //删除巡更任务计划时间信息
    @DELETE
    @Path("plan-times/{timeValue}")
    public Boolean deletePatrolCheckPlanTime(@PathParam("timeValue") Time timeValue) {
        return patrolCheckService.deletePatrolCheckPlanTime(timeValue);
    }

    //获取全部巡更组
    @GET
    @Path("groups")
    public List<PatrolCheckGroupEntity> getAllPatrolCheckGroups() {
        return patrolCheckService.getAllPatrolCheckGroups();
    }

    //获取某个巡更组
    @GET
    @Path("groups/{groupId}")
    public PatrolCheckGroupEntity getPatrolCheckGroup(@PathParam("groupId") Long groupId) {
        return patrolCheckService.getPatrolCheckGroup(groupId);
    }

    //新增某个巡更组
    @POST
    @Path("groups")
    @Consumes(MediaType.APPLICATION_JSON)
    public PatrolCheckGroupEntity createPatrolCheckGroup(PatrolCheckGroupEntity entity) throws AppException {
        return patrolCheckService.createPatrolCheckGroup(entity);
    }

    //修改某个巡更组
    @PUT
    @Path("groups/{groupId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public PatrolCheckGroupEntity updatePatrolCheckGroup(@PathParam("groupId") Long groupId,
                                                         PatrolCheckGroupEntity entity) throws AppException {
        return patrolCheckService.updatePatrolCheckGroup(groupId, entity);
    }

    //删除某个巡更组
    @DELETE
    @Path("groups/{groupId}")
    public Boolean deletePatrolCheckGroup(@PathParam("groupId") Long groupId) {
        return patrolCheckService.deletePatrolCheckGroup(groupId);
    }

    //获取某个巡更组包含的巡更点信息
    @GET
    @Path("groups/{groupId}/check-points")
    public List<PatrolCheckPointView> getPatrolCheckPointList(@PathParam("groupId") Long groupId,
                                                              @QueryParam("cardNumber") Long cardNumber,
                                                              @QueryParam("checkPointName") String checkPointName,
                                                              @QueryParam("floorNumber") Integer floorNumber,
                                                              @QueryParam("position") String position,
                                                              @BeanParam SortParam sortParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (groupId != null) {
            criteria.add(new Criterion("groupId", Criterion.Operator.EQ, groupId));
        }
        if (cardNumber != null) {
            criteria.add(new Criterion("cardNumber", Criterion.Operator.EQ, cardNumber));
        }
        if (checkPointName != null) {
            criteria.add(new Criterion("checkPointName", Criterion.Operator.LIKE, '%' + checkPointName + '%'));
        }
        if (floorNumber != null) {
            criteria.add(new Criterion("floorNumber", Criterion.Operator.EQ, floorNumber));
        }
        if (position != null) {
            if (position.equalsIgnoreCase("isNull")) {
                criteria.add(new Criterion("position", Criterion.Operator.NULL, null));
            } else {
                criteria.add(new Criterion("position", Criterion.Operator.NOT_NULL, null));
            }
        }
        if (sortParam.getOrders() != null && sortParam.getOrders().size() > 0) {
            return patrolCheckService.getPatrolCheckPointList(criteria, Sort.by(sortParam.getOrders()));
        } else {
            return patrolCheckService.getPatrolCheckPointList(criteria, Sort.by(new ArrayList<>()));
        }
    }


    //为某个巡更组增加一个巡更点
    @POST
    @Path("groups/{groupId}/check-points")
    @Consumes(MediaType.APPLICATION_JSON)
    public PatrolCheckPointEntity createPatrolCheckPoint(@PathParam("groupId") Long groupId,
                                                         PatrolCheckPointEntity entity) throws AppException {
        return patrolCheckService.createPatrolCheckPoint(groupId, entity);
    }

    //修改某个巡更点信息
    @PUT
    @Path("groups/{groupId}/check-points/{checkPointId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public PatrolCheckPointEntity updatePatrolCheckPoint(@PathParam("groupId") Long groupId,
                                                         @PathParam("checkPointId") Long checkPointId,
                                                         PatrolCheckPointEntity entity) throws AppException {
        return patrolCheckService.updatePatrolCheckPoint(groupId, checkPointId, entity);
    }

    //删除某个巡更点信息
    @DELETE
    @Path("groups/{groupId}/check-points/{checkPointId}")
    public Boolean deletePatrolCheckPoint(@PathParam("groupId") Long groupId,
                                          @PathParam("checkPointId") Long checkPointId) {
        return patrolCheckService.deletePatrolCheckPoint(groupId, checkPointId);
    }

    //删除某个巡更组下的批量巡更点
    @DELETE
    @Path("groups/{groupId}/check-points")
    @Consumes(MediaType.APPLICATION_JSON)
    public Boolean deletePatrolCheckPoints(@PathParam("groupId") Long groupId, List<Long> checkPointIdList) {
        return patrolCheckService.deletePatrolCheckPoints(groupId, checkPointIdList);
    }


    //分页获取巡更统计结果
    @GET
    @Path("results")
    public Page<PatrolCheckResultEntity> getPatrolCheckResult(@QueryParam("planTime") Time planTime,
                                                              @QueryParam("policeName") String policeName,
                                                              @QueryParam("groupName") String groupName,
                                                              @QueryParam("checkDate") Date checkDate,
                                                              @QueryParam("remark") String remark,
                                                              @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (planTime != null) {
            criteria.add(new Criterion("planTime", Criterion.Operator.EQ, planTime));
        }
        if (groupName != null) {
            criteria.add(new Criterion("groupName", Criterion.Operator.LIKE, '%' + groupName + '%'));
        }
        if (policeName != null) {
            criteria.add(new Criterion("policeName", Criterion.Operator.LIKE, '%' + policeName + '%'));
        }
        if (checkDate != null) {
            java.util.Date startTime = DateUtil.parseDateTimeStr(checkDate.toString() + " 00:00:00");
            java.util.Date stopTime = DateUtil.parseDateTimeStr(checkDate.toString() + " 23:59:59");
            criteria.add(new Criterion("startTime", Criterion.Operator.GTE, startTime));
            criteria.add(new Criterion("startTime", Criterion.Operator.LTE, stopTime));
        }
        if (remark != null) {
            criteria.add(new Criterion("remark", Criterion.Operator.LIKE, '%' + remark + '%'));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return patrolCheckService.getPatrolCheckResult(criteria, pageRequest);
    }

    //分页获取某个巡更统计详细信息
    @GET
    @Path("results/{checkResultId}/details")
    public Page<PatrolCheckResultDetailEntity> getPatrolCheckResultDetails(@PathParam("checkResultId") Long checkResultId,
                                                                           @QueryParam("cardNumber") Long cardNumber,
                                                                           @QueryParam("checkPointName") String checkPointName,
                                                                           @QueryParam("resultStatus") Integer resultStatus,
                                                                           @QueryParam("remark") String remark,
                                                                           @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (checkResultId != null) {
            criteria.add(new Criterion("checkResultId", Criterion.Operator.EQ, checkResultId));
        }
        if (cardNumber != null) {
            criteria.add(new Criterion("cardNumber", Criterion.Operator.EQ, cardNumber));
        }
        if (checkPointName != null) {
            criteria.add(new Criterion("checkPointName", Criterion.Operator.LIKE, '%' + checkPointName + '%'));
        }
        if (resultStatus != null) {
            if (resultStatus.equals(0)) {
                criteria.add(new Criterion("resultStatus", Criterion.Operator.EQ, 0));
            } else if (resultStatus > 0) {
                criteria.add(new Criterion("resultStatus", Criterion.Operator.GT, 0));
            }
        }
        if (remark != null) {
            criteria.add(new Criterion("remark", Criterion.Operator.LIKE, '%' + remark + '%'));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return patrolCheckService.getPatrolCheckResultDetails(criteria, pageRequest);
    }

    //获取某个巡更结果轨迹
    @GET
    @Path("results/{checkResultId}/path-line")
    public List<PatrolCheckResultDetailDto> getPatrolCheckResultPathLine(@PathParam("checkResultId") Long checkResultId) {
        return patrolCheckService.getPatrolCheckResultPathLine(checkResultId);
    }

    //添加/修改巡更点位置
    @POST
    @Path("points/{pointId}/position")
    public PatrolCheckPointView createPatrolCheckPointPosition(@PathParam("pointId") Long pointId,
                                                               PatrolCheckPointPositionEntity positionEntity)
            throws AppException {
        return patrolCheckService.createPatrolCheckPointPosition(pointId, positionEntity);
    }

    //删除巡更点位置
    @DELETE
    @Path("points/{pointId}/position")
    public Boolean deletePatrolCheckPointPosition(@PathParam("pointId") Long pointId) throws AppException {
        return patrolCheckService.deletePatrolCheckPointPosition(pointId);
    }
}
